# include <iostream>
# include <array>
# include <algorithm>
# include "GameFunction.h"
# include <unistd.h>
using namespace std;

void display_board(int (*chess_state)[15]) {
    cout << "  ";
    for (int i = 0; i < 15; i++) {
        if (i < 10) cout << i << "  ";
        else cout << i << " ";
    }
    cout << endl;
    for (int i = 0; i < 15; i++) {
        if (i < 10) {
            cout << " ";
        }
        cout << i;
        for (int j = 0; j < 15; j++) {
            if (chess_state[i][j] == 0) {
                cout << "+";
            }
            else if (chess_state[i][j] == 1) {
                cout << "A";
            }
            else {
                cout << "B";
            }
            if (j < 14) cout << "--";
        }
        cout << endl;
    }
}

int main() {
    // 对博弈树的定义
    char game_start;
    cout << "game start?" << endl;
    cin >> game_start;
    int board[15][15] = {0};
    board[7][7] = 1;
    while (game_start == 'y') {
        display_board(board);
        array<int, 2> new_position = game_next_step(board, 1);
        if (new_position[0] == -1) {
            break;
        }
        else {
            if (board[new_position[0]][new_position[1]] == 0) {
                board[new_position[0]][new_position[1]] = 1;
                int result = win_lose_judge(board, 1);
                if (result == 1) {
                    cout << "you win" << endl;
                    for (auto & i : board) {
                        for (int & j : i) {
                            j = 0;
                        }
                    }
                    continue;
                }
                usleep(10000);
                array<int, 2> machine_new_pos = game_next_step(board, 2);
                board[machine_new_pos[0]][machine_new_pos[1]] = 2;
                result = win_lose_judge(board, 2);
                if (result == 2) {
                    cout << "machine wins" << endl;
                    for (auto & i : board) {
                        for (int & j : i) {
                            j = 0;
                        }
                    }
                    continue;
                }
            }
            else {
                cout << "repeated input" << endl;
                continue;
            }
        }
    }
    return 0;
}
